package com.bric.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.log4j.Logger;

public class HttpUtil {
	private static final 	Logger log=Logger.getLogger(HttpUtil.class);

	public static String post(String jsonParam,String url){
		StringBuffer answer = new StringBuffer();
		try{ 
		URL u = new URL(url);
         URLConnection conn = u.openConnection();
         conn.setDoOutput(true);
         OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
         
         //write parameters
         if(null!=jsonParam){
        	 writer.write(jsonParam);
         }
         writer.flush();
         
         // Get the response
         
         BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line;
         while ((line = reader.readLine()) != null) {
             answer.append(line);
         }
         writer.close();
         reader.close();
         
         //Output the response
//         System.out.println(answer.toString());
         
     } catch (MalformedURLException ex) {
         log.info(ex.getMessage());
     } catch (IOException e) {
        log.info(e.getMessage());
     }
		return answer.toString();
	}
	public static boolean downLoadFile(String urlStr,String filePath,String fileName){
        HttpURLConnection conn=null;
		boolean flag=true;
        try {
			URL url = new URL(urlStr);    
			conn = (HttpURLConnection)url.openConnection();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			log.info(e1.getMessage());
			flag=false;
		}    
                //设置超时间为3秒  
        conn.setConnectTimeout(3*1000);  
        //防止屏蔽程序抓取而返回403错误  
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
        FileOutputStream fos=null;
        InputStream inputStream=null;
        try {
        //得到输入流  
        inputStream = conn.getInputStream();    
        //获取自己数组  
        byte[] getData = readInputStream(inputStream);      
  
        //文件保存位置  
        File saveDir = new File(filePath);  
        if(!saveDir.exists()){  
            saveDir.mkdir();  
        }  
        File file = new File(saveDir+File.separator+fileName);      
        fos = new FileOutputStream(file);       
        fos.write(getData);   
        
        } catch (Exception e) {
			flag=false;
		}finally{
			if(fos!=null){  
	            try {
					fos.close();
				} catch (IOException e) {
					log.info(e.getMessage());
				}    
	        }  
	        if(inputStream!=null){  
	            try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	        } 
		}
		return flag;
	}
	private static byte[] readInputStream(InputStream inputStream) {
		byte[] bytes=new byte[1024];
		ByteArrayOutputStream boa=new ByteArrayOutputStream();
		BufferedInputStream bis=new BufferedInputStream(inputStream);
		int len=0;
		try {
			while((len=bis.read(bytes))!=-1){
				boa.write(bytes, 0, len);
			}
		} catch (IOException e) {
			log.info(e.getMessage());
		}
		return boa.toByteArray();
	}
	public static void main(String[] args) {
		downLoadFile("http://www.ncpqh.com/report/20170222095555.swf", "D:/fagaiwei/temp/reportFile/", "test.swf");
	}
}
